package com.github.chirspan.xaas.api;

import com.github.chirspan.xaas.log.annotation.ApiLog;
import com.github.chirspan.xaas.core.rest.RestResult;
import com.github.chirspan.xaas.mybatis.service.IBaseService;
import com.github.chirspan.xaas.mybatis.util.tree.Tree;
import com.github.chirspan.xaas.mybatis.util.tree.TreeUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author chenpan
 * @ClassName: BaseCRUDRestController
 * description: 基础CRUD 控制器
 * @date 2018-04-03 17:30
 */
public abstract class BaseCRUDTreeRestController<T extends Tree> extends BaseCRUDRestController {

    @Autowired
    protected IBaseService<T> baseService;

    @GetMapping("/_tree")
    @ApiOperation(value = "查询", authorizations = {@Authorization(value = "apiKey")})
    public RestResult treeList(@ApiParam("查询条件实体") T m) throws IllegalAccessException {

        List<T> rs = baseService.selectByWrapper(BaseCRUDWrapper.query(m));
        return RestResult.OK(TreeUtils.buildTree(rs));
    }

    @Override
    @DeleteMapping("/{id:.+}")
    @ApiOperation(value = "根据ID删除", authorizations = {@Authorization(value = "apiKey")})
    @ApiLog(value = "根据ID删除")
    public RestResult delete(@PathVariable("id") String id) {
        baseService.recursiveDelete(id);
        return RestResult.OK(id, "删除成功");
    }
}
